package com.utils.office;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * Created by rain on 2017/4/18.
 */
public class PPTConvertToHtml {

    private static final Integer SEPARATE_DISTANCE = 100;

    public static void convertPPTXToImage(String fileUrl)
            throws IOException {
        URL url = new URL(fileUrl);

        XMLSlideShow ppt = new XMLSlideShow(url.openConnection().getInputStream());

        Dimension pgsize = ppt.getPageSize();
        System.out.println(pgsize.width+"--"+pgsize.height);

        XSLFSlide[] slides = ppt.getSlides();
        for (int i = 0; i < slides.length; i++) {
            try {
                //防止中文乱码
                for(XSLFShape shape : slides[i].getShapes()){
                    if(shape instanceof XSLFTextShape) {
                        XSLFTextShape tsh = (XSLFTextShape)shape;
                        for(XSLFTextParagraph p : tsh){
                            for(XSLFTextRun r : p){
                                r.setFontFamily("宋体");
                            }
                        }
                    }
                }
                BufferedImage image = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = image.createGraphics();
                // clear the drawing area
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

                // render
                slides[i].draw(graphics);

                ByteOutputStream stream = new ByteOutputStream();
                ImageIO.write(image, "jpg", stream);
                stream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * pptx
     * @throws IOException
     */
    public static void convertPPT2Image(String fileUrl)
            throws IOException {
        URL url = new URL(fileUrl);

        InputStream is = url.openConnection().getInputStream();
        try {
            SlideShow ppt = new SlideShow(is);
            Dimension pgSize = ppt.getPageSize();
            Slide[] slide = ppt.getSlides();

            for (int i = 0; i < slide.length; i++) {
                TextRun[] textRuns = slide[i].getTextRuns();

                for (int k = 0; k < textRuns.length; k++) {
                    RichTextRun[] richTextRuns = textRuns[k].getRichTextRuns();
                    for (int j = 0; j < richTextRuns.length; j++) {
                        richTextRuns[j].setFontIndex(1);
                        richTextRuns[j].setFontName("宋体");
                    }
                }

                BufferedImage image = new BufferedImage(pgSize.width, pgSize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = image.createGraphics();
                graphics.fill(new Rectangle2D.Float(0, 0, pgSize.width, pgSize.height));
                slide[i].draw(graphics);

                ByteOutputStream stream = new ByteOutputStream();
                ImageIO.write(image, "jpg", stream);
                stream.close();
                is.close();
            }
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
